//
//  MyViewController.swift
//  XMYouPin
//
//  Created by 王宗成 on 2019/7/2.
//  Copyright © 2019 王宗成. All rights reserved.
//

import UIKit

class MyViewController: UIViewController {

    lazy var topNavView:WRCustomNavigationBar = {
        
        return WRCustomNavigationBar.CustomNavigationBar()
    } ()
    
    lazy var tableView:UITableView = {
        
        let tab:UITableView = UITableView(frame: CGRect.init(x: 0, y: 0, width: kScreenH, height: kScreenH), style: .grouped)
        tab.separatorStyle = UITableViewCell.SeparatorStyle.none
        tab.register(HomeBannerTableViewCell.self, forCellReuseIdentifier: HomeBannerTableViewCell.identifier())
        tab.register(HomeTodayActivityTableViewCell.self, forCellReuseIdentifier: HomeTodayActivityTableViewCell.identifier())
        tab.register(HomeRecommendTableViewCell.self, forCellReuseIdentifier: HomeRecommendTableViewCell.identifier())
        tab.register(Crowdfund_TableViewCell.self, forCellReuseIdentifier: Crowdfund_TableViewCell.identifier())
        tab.register(HomeSingleImgTableViewCell.self, forCellReuseIdentifier: HomeSingleImgTableViewCell.identifier())
        
        tab.register(PlazaTableViewCell.self, forCellReuseIdentifier: PlazaTableViewCell.identifier())
        
        tab.register(HotRecommendTableViewCell.self, forCellReuseIdentifier: HotRecommendTableViewCell.identifier())
        
        tab.register(PersonCustomerTableViewCell.self, forCellReuseIdentifier: PersonCustomerTableViewCell.identifier())
        
        
        tab.register(PersonActivityTableViewCell.self, forCellReuseIdentifier: PersonActivityTableViewCell.identifier())
        
        tab.register(HotSellTableViewCell.self, forCellReuseIdentifier: HotSellTableViewCell.identifier())
        
        tab.register(WZC_TableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: WZC_TableViewHeaderFooterView.identifier())
        
        
        tab.register(WZC_TableViewCell.self, forCellReuseIdentifier: WZC_TableViewCell.identifier())
        return tab
    }()
    
    var recmmendModel:RecommendModel?
    
    func loadData() -> RecommendModel? {
        
        let path = Bundle.main.path(forResource: "Recommend", ofType: "json")
        // 2. 获取json文件里面的内容,NSData格式
        let jsonData=NSData(contentsOfFile: path!)
        // 3. 解析json内容
        
        do {
            
            let json = try JSONSerialization.jsonObject(with: jsonData! as Data, options: .allowFragments)
            
            if let result = json as? Dictionary<String, Any> {
                
                return  RecommendModel.deserialize(from: result)
                
            }
            else {
                
                return nil
            }
          
        } catch  {
            
            print("解析失败")
            return nil
        }
    }
    
    lazy var settButton:UIButton = {
        
        return UIButton(type: .custom)
    } ()
    lazy var tableHeaderView:PersonTableHeaderView = {
        
        let view:PersonTableHeaderView = PersonTableHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenW, height: kPersonHeaderHeight))
  
        return view
    } ()
    
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        self.recmmendModel = loadData()
        
        
        layoutSubViewUI()
   
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.navigationBar.isHidden = true
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        self.navigationController?.navigationBar.isHidden = false
        statusBarStyle = UIStatusBarStyle.default
    }
}

extension MyViewController:UITableViewDelegate,UITableViewDataSource
{
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
//         print(scrollView.contentOffset.y)
        
        if scrollView.contentOffset.y > kNavigationBarHeight {
            
            self.topNavView.wr_setBackgroundAlpha(alpha: 1)
            statusBarStyle = UIStatusBarStyle.default
            self.topNavView.wr_setRightButton(image: IMAGE("mjyp_navi_main_page_msg_normal"))
            self.settButton.setImage(IMAGE("set_icon_set_deep"), for: .normal)
        }
        else
        {
            self.topNavView.wr_setBackgroundAlpha(alpha: 0)
            
            statusBarStyle = UIStatusBarStyle.lightContent
            self.topNavView.wr_setRightButton(image: IMAGE("mjyp_set_icon_message"))
            self.settButton.setImage(IMAGE("mjyp_set_icon_set"), for: .normal)
        }
        
        if scrollView.contentOffset.y < 0 {

            let totalOffset:CGFloat = kPersonHeaderImgViewHeight + abs(scrollView.contentOffset.y)+20

            self.tableHeaderView.imgView.frame = CGRect(x: 0, y: scrollView.contentOffset.y, width: kScreenW, height: totalOffset)

        }
      
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section==0 {
            
            return 0.1
        }
        else
        {
            return 40.0;
        }
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        if section==0 {
            
            return UIView()
        }
        else
        {
            
            let view:WZC_TableViewHeaderFooterView = tableView.dequeueReusableHeaderFooterView(withIdentifier: WZC_TableViewHeaderFooterView.identifier()) as! WZC_TableViewHeaderFooterView
            view.titleLable.font = BFont(19)
            view.backgroundColor = UIColor.hexColor(hexadecimal: "#f2f2f2")
            view.titleLable.text = "专属推荐";
            return view;
            
        }
        
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 0 {
            
            return 3
        }
        else
        {
            return 1
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            
            if indexPath.row == 0{
                
                let rate:CGFloat =  1080.0/210.0
                return (kScreenW - 2*0)/rate + 20
            }
            else if indexPath.row == 1{
                
                return 90
            }
            else
            {
                return 180
            }
            
        }
        else
        {
        
            let count : Int = self.recmmendModel?.data?.count ?? 0
            
            var row = 0;
            
            if count%2 == 0{
                
                row = count/2
            }
            else
            {
                row = count/2 + 1
            }
            
            return CGFloat(row*64) * 4.5 +  CGFloat(row*10 +  20)
        }
        
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            
            if indexPath.row == 0{
                
                let cell:HomeSingleImgTableViewCell = tableView.dequeueReusableCell(withIdentifier: HomeSingleImgTableViewCell.identifier(), for: indexPath) as! HomeSingleImgTableViewCell
              
                cell.imgView.kf.setImage(with: URL(string: "https://img.youpin.mi-img.com/yingkebao/d4b0aa8d88ab97bb8b20045841d7df15.png"))
                
                return cell;
            }
            else if indexPath.row == 1{
                
                
                let cell:PersonActivityTableViewCell = tableView.dequeueReusableCell(withIdentifier: PersonActivityTableViewCell.identifier(), for: indexPath) as! PersonActivityTableViewCell
            
                return cell;
                
            }
            else
            {
                let cell:PersonCustomerTableViewCell = tableView.dequeueReusableCell(withIdentifier: PersonCustomerTableViewCell.identifier(), for: indexPath) as! PersonCustomerTableViewCell
                
                return cell;
            }
            
        }
        else
        {
            
            let cell:HotRecommendTableViewCell = tableView.dequeueReusableCell(withIdentifier: HotRecommendTableViewCell.identifier(), for: indexPath) as! HotRecommendTableViewCell
            cell.model = self.recmmendModel
            return cell
        }
    }
}


extension MyViewController
{
    func layoutSubViewUI() -> Void {
        

        
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.tableHeaderView = self.tableHeaderView
        view.addSubview(self.tableView)
        self.tableView.snp.makeConstraints { (make) in
            
            make.edges.equalTo(UIEdgeInsets.zero)
        }

        self.view.addSubview(self.topNavView)

        self.topNavView.wr_setBottomLineHidden(hidden: true)
        self.topNavView.wr_setBackgroundAlpha(alpha: 0)
        self.topNavView.wr_setRightButton(image: IMAGE("mjyp_set_icon_message"))
        self.settButton.setImage(IMAGE("mjyp_set_icon_set"), for: .normal)
        self.topNavView.addSubview(self.settButton)

        self.settButton.snp.makeConstraints { (make) in

            make.right.equalTo(self.topNavView.rightButton.snp_left).offset(-10)
            make.centerY.equalTo(self.topNavView.rightButton.snp_centerY)
        }
      
      
    }
}
